package com.example.common;

public class Board {
    private static final int n = 3, N = 9;
    private static final char notNumber = '.';
    private final char[][] board;
    private int[] rowCount;
    private int[] columnCount;
    private int[][] squareCount;

    public Board(char[][] board) {
        this.board = board;
        this.rowCount = new int[N];
        this.columnCount = new int[N];
        this.squareCount = new int[n][n];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (board[i][j] != notNumber) {
                    rowCount[i]++;
                    columnCount[j]++;
                    squareCount[i/3][j/3]++;
                }
            }
        }
    }

    public char[][] getBoard() {
        return board;
    }

    public int[] getRowCount() {
        return rowCount;
    }

    public int[] getColumnCount() {
        return columnCount;
    }

    public int[][] getSquareCount() {
        return squareCount;
    }

    @Override
    public String toString() {
        int sum = getNumberCount();
        StringBuilder sb = new StringBuilder();
        sb.append("初始数字").append(sum).append("个");
        sb.append("\n");
        for (int i = 0; i < board.length; i++) {
            sb.append(board[i]).append("\n");
        }
        return sb.toString();
    }

    public int getNumberCount() {
        int sum = 0;
        for (int x : rowCount) {
            sum += x;
        }
        return sum;
    }

    public String join() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < board.length; i++) {
            sb.append(board[i]);
        }
        return sb.toString();
    }

    public void insert(int x, int y, char c) {
        board[x][y] = c;
        rowCount[x]++;
        columnCount[y]++;
        squareCount[x/3][y/3]++;
    }
}
